﻿/*----------------------------------------------------------------
// Copyright (C) ET
// 版权所有。 
//
// 文件名：RTDBLink

// 文件功能描述：DB2操作

// 创建标识：胡进财 20140822
 
// 修改标识：

// 修改描述：

//----------------------------------------------------------------*/
using ET.TextHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET.Monitor.OperationDB
{
    public class RTDBLink
    {
        /// <summary>
        /// 数据库操作类，外部调用数据库操作方法时直接调用在此类中定义的方法
        /// </summary>
        string rlDBType = "";
        RTDBHelper rtdbLink;
        public RTDBLink(string path, string realtype, string name)
        {
            string DBtype = this.init(path, realtype, name);
            if (DBtype == "PI")
            {
                rtdbLink = new Pi(path, realtype, name);
            }
            else if (DBtype == "Mongo")
            {
                rtdbLink = new Mongo(path, realtype, name);
            }
            else if (DBtype == "SQL")
            {
                rtdbLink = new SQL(path, realtype, name);
            }
        }
        /// <summary>
        /// 打开pi数据库连接
        /// </summary>
        public int OpenSR(ref int handle)
        {
            return rtdbLink.OpenSR(ref handle);
        }
        /// <summary>
        /// 关闭pi数据库连接
        /// </summary>
        public object CloseSR(int handle)
        {
            return rtdbLink.closeSR(handle);
        }
        /// <summary>
        /// 获取实时值
        /// </summary>
        public object GetRealTimeValue(string tagName, int handle)
        {
            return rtdbLink.GetRealTimeValue(tagName, handle);
        }
        /// <summary>
        /// 获取实时值数组
        /// </summary>
        public double[] GetRealTimeValues(int count, string[] tagName, int handle)
        {
            return rtdbLink.GetRealTimeValues(count, tagName, handle);
        }
        /// <summary>
        /// 获取历史值
        /// </summary>m
        public double GetHisValue(string tagName, string hisTime, int handle)
        {
            return rtdbLink.GetHisValue(tagName, hisTime, handle);
        }
        /// <summary>
        /// 写实时值
        /// </summary>
        public int SetRealTimeValue(ref string tagName, ref object val, int handle)
        {
            return rtdbLink.SetRealTimeValue(ref tagName, ref val, handle);
        }
        /// <summary>
        /// 写历史值
        /// </summary>
        public int SetHisValue(ref string tagName, ref string hisTime, ref object val, int handle)
        {
            return rtdbLink.SetHisValue(ref tagName, ref hisTime, ref val, handle);
        }

        public void GetHisValue(string pName, string time, ref double val)
        {
            rtdbLink.GetHisValue(pName, time, ref val);
        }
        public virtual double[] GetHisValue(string tag, DateTime sTime, DateTime eTime)
        {
            return rtdbLink.GetHisValue(tag, sTime, eTime);
        }
        public virtual double[] GetHisValue(string tag, DateTime sTime, DateTime eTime, int second)
        {
            return rtdbLink.GetHisValue(tag, sTime, eTime, second);
        }

        public virtual DataTable GetHisValDt(string tag, DateTime sTime, DateTime eTime)
        {
            return rtdbLink.GetHisValDt(tag, sTime, eTime);
        }
        public void GetHisValue(string pName, string date, ref int ret, ref double val)
        {
            rtdbLink.GetHisValue(pName, date, ref ret, ref val);
        }
        public void GetHisValueByTime(string pName, string date, ref int ret, ref double val)
        {
            rtdbLink.GetHisValueByTime(pName, date, ref ret, ref val);
        }
        public void GetHisDiffValue(string pName, string bTime, string eTime, ref int ret, ref double val)
        {
            rtdbLink.GetHisDiffValue(pName, bTime, eTime, ref ret, ref val);
        }
        public void GetHisDiffValue(string pName, string bTime, ref int ret, ref double val)
        {
            rtdbLink.GetHisDiffValue(pName, bTime, ref ret, ref val);
        }
        public void GetHisDiffValueByTime(string pName, string bTime, string eTime, ref int ret, ref double val)
        {
            rtdbLink.GetHisDiffValueByTime(pName, bTime, eTime, ref ret, ref val);
        }
        public string GetPoint()
        {
            return rtdbLink.GetPoint();
        }
        public int DateToUTC()
        {
            return rtdbLink.DateToUTC();
        }
        public string RetPointValueAvg(string tag, int type, string date)
        {
            return rtdbLink.RetPointValueAvg(tag, type, date);
        }
        public string RetPointDiffValue(string tag, string bt, string et)
        {
            return rtdbLink.RetPointDiffValue(tag, bt, et);
        }

        public string init(string path, string realtype, string name)
        {
            rlDBType = IniHelper.ReadIniData(realtype, "DBType", path, name);
            return rlDBType;
        }
        /// <summary>
        /// 获取单个标签点一段时间内等间隔历史插值
        /// 创建：李东峰 日期：2014-05-06
        /// 修改：
        /// 修改内容：
        ///
        /// </summary>
        /// <param name="count">想要获取的历史值的个数</param>
        /// <param name="tagName">标签点名称</param>
        /// <param name="startTime">起始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="handle">数据库连接句柄</param>
        /// <returns>返回取到的历史值数组</returns>
        public double[] GetHisValueByStartTimeAndEndTime(int count, string tagName, string startTime, string endTime, int handle)
        {
            return rtdbLink.GetHisValueByStartTimeAndEndTime(count, tagName, startTime, endTime, handle);
        }
    }
}
